package cn.mdmm.user.service.impl.news;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;

import cn.mdmm.user.common.PageUtils;
import cn.mdmm.user.entity.news.NewsInfoModel;
import cn.mdmm.user.entity.news.request.NewsInfoRequest;
import cn.mdmm.user.entity.response.ResponseBodyToGridData;
import cn.mdmm.user.entity.response.ResponseBodyToOne;
import cn.mdmm.user.entity.type.TypeInfoModel;
import cn.mdmm.user.mapper.news.NewsInfoMapper;
import cn.mdmm.user.service.news.NewsInfoService;
import cn.mdmm.user.service.system.SysDictService;
import cn.mdmm.user.service.type.TypeInfoService;
import cn.modoumama.common.exception.RequiredException;
import cn.modoumama.common.response.Response;
import cn.modoumama.page.GridDataModel;
import cn.modoumama.page.Page;
import cn.modoumama.service.base.impl.BaseServiceImpl;


/**
* 类名称：NewsInfo对象定义<br>
* 类描述：新闻业务实现类<br>
* 类生产方式：工具自动生成代码<br>
* 创建人：éå¼º<br>
* 创建时间：2017-06-02 09:31:54<br>
* 修改人：  <br>
* 修改时间：<br>
* 修改备注：<br>
* @version   V1.0
*/

@Service(version = "1.0")
public class NewsInfoServiceImpl extends BaseServiceImpl<NewsInfoModel,Long> implements NewsInfoService{
	Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private TypeInfoService typeInfoService;
	@Autowired
    private SysDictService sysDictService;
	@Autowired
	private NewsInfoMapper currMapper;
	
	@Autowired
	public void setMapper(NewsInfoMapper genericMapper) {
	super.setGenericMapper(genericMapper);
	}
	
	//------------------请在此添加自定义方法（开始）------------------
	public GridDataModel getGridDataModelByCondition(Page po){
		Integer type = (Integer) po.getCondition().get("type");
		List<Integer> types = new ArrayList<Integer>();
		if(type != null){
			types.add(type);
			TypeInfoModel typeInfo = typeInfoService.getTypeById(type);
			types.addAll(typeInfo.getChildrenIds());
			po.addCondition("types", types);
			po.getCondition().remove("type");
		}
		Integer totalCount = countByCondition(po.getCondition());
		RowBounds rowBounds = new RowBounds(po.getOffset(), po.getPageSize());
		List<NewsInfoModel> results = currMapper.findListByCondition(po.getCondition(), rowBounds);
		return new GridDataModel(results, totalCount.intValue());
	}
	
	@Override
	public Integer insert(NewsInfoModel entity) {
		int count = 0;
		Map<String, String> languageMap = sysDictService.getDetailValueMap("语言");
		if(entity.getLanguage()!= null){
			for (Entry<String, String> language : languageMap.entrySet()) {
				entity.setId(null);
				entity.setLanguage(Integer.parseInt(language.getKey()));
				count = super.insert(entity);
			}
		}
		return count;
	}

	@Override
	public Response getNewsList(NewsInfoRequest request) {
		ResponseBodyToGridData gridData = new ResponseBodyToGridData();
		Integer pageSize = request.getRows();
		pageSize = pageSize == null ? 3 : pageSize;
		Page po = PageUtils.getPage(request,"PUBLISH_DATE DESC");
		po.addCondition("status", 2);
		po.setPageSize(pageSize);
		gridData.setData(getGridDataModelByCondition(po));
		return gridData;
	}
	
	@Override
	public Response getNewsInfo(NewsInfoRequest request) {
		ResponseBodyToOne bodyToOne = new ResponseBodyToOne();
		if(request.getId()==null){
			throw new RequiredException("id");
		}
		bodyToOne.setData(getNewsInfo(request.getId()));
		return bodyToOne;
	}
	
	@Override
	public NewsInfoModel getNewsInfo(Long id){
		currMapper.addBrowseNum(id);
		return findById(id);
	}
	
	@Override
	public Integer updateNewsInfo(Integer[] ids, NewsInfoModel newsInfo, Long editor) {
		return currMapper.updateNewsInfos(ids, newsInfo, editor);
	}
	//------------------请在此添加自定义方法（结束）------------------
}
